package com.yanqu.road.dao.impl.activity.musicroomrank;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.musicroomrank.MusicRoomRankConfigRankData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MusicRoomRankConfigDaoImpl extends TempDao {
    public static String getMusicRoomRankConfigSql = "select * from `t_s_activity_musicroom_rank` where `activityId`=?";
    public List<MusicRoomRankConfigRankData> getRankConfigList(int activityId) {
        List<MusicRoomRankConfigRankData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getMusicRoomRankConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                MusicRoomRankConfigRankData data = parseConfigData(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    private MusicRoomRankConfigRankData parseConfigData(ResultSet rs) throws SQLException {
        MusicRoomRankConfigRankData data = new MusicRoomRankConfigRankData();
        data.setActivityId(rs.getInt("activityId"));
        data.setId(rs.getInt("id"));
        data.setName(rs.getString("name"));
        data.setDesc(rs.getString("desc"));
        data.setRank(rs.getInt("rank"));
        data.setScore(rs.getInt("score"));
        data.setReward(rs.getString("rewards"));
        data.setIcon(rs.getString("icon"));
        return data;
    }
}
